// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
	'Vsg.Component.AttentionBlockGrid'
	]
);

Vsg.loadExtLibs([
	"miframe.js"
	]
);

/** ===============================
 * init ui interface 
 * ================================
 */
function init(){

	try {
		
		var $tabsArray = [];

		$tabsArray.push({
			title:"热点种类/板块",
			items:[{
				id:"if1",
				header: false,
				xtype:"iframepanel",
				height:200,
				border:false,
				loadMask:true,
				frameConfig:{
					autoCreate:{
						id:"charts" , name:"charts",frameborder:0
					}},
				defaultSrc:UrlMKey['assembly-charts']
			}],
			layout:"fit",
			id:"tab1"
		});

		$tabsArray.push({
			title:"种类／板块频率",
			items:[{
				id:"if2",
				xtype:"iframepanel",
				height:200,
				header: false,
				border:false,
				loadMask:true,
				frameConfig:{
					autoCreate:{
						id:"frequency" , 
						name:"frequency",
					frameborder:0}},
				defaultSrc:UrlMKey['assembly-frequency']
			}],
			layout:"fit",
			id:"tab2"
		});




		var $tabs = new Ext.TabPanel({
			region:"center",
			id:'mainPanel',
			width:250,
			activeTab:0,
			tbar:[
				{
					text:"添加关注板块" , 
					iconCls: "bmenu",
					handler:Vsg.getDefinedEvent("evt_add_attention_block")
				},
				{
					text:"移除关注板块" , 
					iconCls: "bmenu",
					handler:Vsg.getDefinedEvent("evt_remove_attention_block")
				}				
			],
			frame:false,
			margins:{left:8,top:4,right:8,bottom:8},
			items:$tabsArray,
			plain:true
		});
	
		var $layoutBox = [
			$tabs
		];
		
		// --- set layout content -----
		var $layoutContent = {
			layout:"border",
			items:$layoutBox
		};

    	 new Ext.Viewport( $layoutContent );
		
	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
		//Ext.log("Initial UI error: " + e);
	}
}
// --- run the init method
Vsg.run( init );


/**
 * event
 * @param {Object} $this
 * @param {Object} rowIndex
 * @param {Object} event
 */

Vsg.definedEvent("evt_add_attention_block" , function($this, e) {
	var tabPanel = Ext.getCmp("mainPanel");
	
	// --- get the select record ---
	var actPanel = tabPanel.getActiveTab();
	var childObj = actPanel.find("xtype","iframepanel")[0];
	var iframeWin = childObj.getFrameWindow(); 

	var records = iframeWin.getSelectedCategoryRecords();

	try {
		// load config object 

		var $actionConfig = {
			url:UrlMKey['assembly-attention-saved'],
			method:"POST",
			timeout:30000,
			params:{
				records:Ext.util.JSON.encode(records)
			}
		};
		$actionConfig.success = function(response , opts) {
			var res =Ext.decode(response["responseText"]);
			Ext.MessageBox.show({
				title: "提示",
				msg: res.msg,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.WARNING
			});
		}


		$actionConfig.failure = function(response , opts) {
			var res =Ext.decode(response["responseText"]);			
			Ext.MessageBox.show({
				title: "提示",
				msg: res.msg,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.WARNING
				});	
		}

		
		Ext.Ajax.request($actionConfig);
		
	} catch ( ex ) {
		alert( ex );
	}	
});


Vsg.definedEvent("evt_remove_attention_block" , function($this, e) {
	var $buttons = [];	
	// ----- defined button 
	var $btn = null;
	$btn = {
		text:"删 除",
		handler: function(){
		
			var $records = attenBlockGrid.getSelectionModel().getSelections();
			
			var ids = "";
			for (var $i = 0; $i < $records.length; $i++) {
				if ($i != 0) {
					ids+=",";
				}
				ids+=$records[$i].id;
			}

			try {
				// load config object 

				var $actionConfig = {
				url:UrlMKey['assembly-attention-remove'],
				method:"POST",
				timeout:30000,
				params:{
					records:ids
					}
				};
				$actionConfig.success = function(response , opts) {
				var res =Ext.decode(response["responseText"]);
					Ext.MessageBox.show({
						title: "提示",
						msg: res.msg,
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.WARNING
					});
					
					var store = attenBlockGrid.getStore();
					store.removeAll();
					store.reload();
				}


				$actionConfig.failure = function(response , opts) {
					var res =Ext.decode(response["responseText"]);			
					Ext.MessageBox.show({
						title: "提示",
						msg: res.msg,
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.WARNING
					});	
				}

		
				Ext.Ajax.request($actionConfig);
		
			} catch ( ex ) {
				alert( ex );
			}
			
		}	

	};
	$buttons.push( $btn );

	$btn = {
		text:"关 闭",
		handler:function() {
			if (win != null) {
				win.hide();
			}
		}
	}
	$buttons.push( $btn );	
			
	var attenBlockGrid = new Vsg.Component.AttentionBlockGrid({
		url: UrlMKey['attention-blocks'],
		id:'attentionCate'
	})         
	var win = new Ext.Window({
		title:'关注板块/种类',
		closable:true,
        width:600,
        height:350,
        //border:false,
        plain:true,
        layout: 'fit',
		buttons:$buttons,
        items: [attenBlockGrid]
    });

    win.show(this);
});
